import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/Home.vue'
import store from '@/store'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView
  },
  {
    path: '/goods',
    name: 'goods',
    component: () => import('../views/Goods.vue')
  },
  {
    path: '/user',
    name: 'user',
    component: () => import('../views/User.vue'),
    redirect:'/user/cart',
    children:[{
      path:'cart',
      component: () => import('../components/user/Cart.vue'),
    }]
  },
  {
    path: '/order',
    name: 'order',
    component: () => import('../views/Order.vue')
  },
  {
    path: '/free',
    name: 'free',
    component: () => import('../views/Free.vue')
  },
  {
    path: '/details',
    name: 'details',
    component: () => import('../views/Details.vue')
  },
  {
    path: '*',
    name: 'error',
    component: () => import('../components/Error.vue')
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => {})
};


// // 全局导航守卫
// router.beforeEach((to, from, next)=>{
//   // 有token就表示已经登录
//   // 想要进入个人中心页面，必须有登录标识token
    
//   // console.log('to:', to)
//   // console.log('from:', from)
//   if(to.path=='/user'){
//      	let token = localStorage.getItem('x-auth-token')
//         // 此时必须要有token
//         if(token){
//           next(); // next()去到to所对应的路由界面
//         }else{
//           // 提示没有登录
//           store.dispatch("showToast/asyncIsShowToast",{
//             isshow:true,
//               msg: "你还没有登录！",
//               type: "danger",
//           })
//         }
//         return; // 需要些return，防止执行完上面的next()，还继续执行下面的next()
//   }
 
//   // 如果不是去往个人中心的路由，则直接通过守卫，去到to所对应的路由界面
//   next()
// })

export default router